﻿// -----------------------------------------------------------------------
// <copyright file="InputOutput.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Exc2
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class UIInputOutput
    {
        private const int k_LengthOfLegalMoveString = 5;

        public static string GetPlayerName()
        {
            Console.WriteLine("Please enter player name: ");
            return Console.ReadLine();
        }

        public static int GetBoardSize()
        {
            Console.WriteLine("Please enter game board size (6,8 or 10): ");
            return int.Parse(Console.ReadLine());
        }

        public static byte GetNumberOfPlayers()
        {
            string userInput = null;
            do
            {
                Console.WriteLine("Please enter number of players (1 or 2): ");
                userInput = Console.ReadLine();
            }
            while (userInput != "1" && userInput != "2");
            return byte.Parse(userInput);
        }

        public static CheckersMove GetMoveFromPlayer(Player i_currentTurnPlayer, ref bool quitting, ref string i_previousPlayerMove)
        {
            Console.WriteLine(i_currentTurnPlayer.Name + "'s turn: ");

            string moveFromUser = Console.ReadLine();
            while (!CheckStringLegitimacy(moveFromUser) && !quitting)
            {
                if (moveFromUser == "Q")
                {
                    quitting = true;
                    break;
                }

                Console.WriteLine("Illegal input format/size!");
                moveFromUser = Console.ReadLine();
            }

            CheckersMove currentMove = new CheckersMove(); 
            if (!quitting)
            {
                 currentMove = CheckersGame.InterpretateStringToMove(moveFromUser, i_currentTurnPlayer);

                while (!CheckersGame.isInputMoveLegal(currentMove, i_currentTurnPlayer) && !quitting)
                {
                    Console.WriteLine("Illegal input move, please try again: ");
                    moveFromUser = Console.ReadLine();
                    currentMove = CheckersGame.InterpretateStringToMove(moveFromUser, i_currentTurnPlayer);
                }
            }

            i_previousPlayerMove = moveFromUser;
            return currentMove;
        }

        private static bool CheckStringLegitimacy(string i_stringToCheck)
        {
            bool isLegal;
            if (i_stringToCheck.Length == k_LengthOfLegalMoveString && i_stringToCheck[2] == '>')
            {
                if ((i_stringToCheck[0] >= 'A' && i_stringToCheck[0] <= 'Z') && (i_stringToCheck[3] >= 'A' && i_stringToCheck[3] <= 'Z'))
                {
                    if ((i_stringToCheck[1] >= 'a' && i_stringToCheck[1] <= 'z') && (i_stringToCheck[4] >= 'a' && i_stringToCheck[4] <= 'z'))
                    {
                        isLegal = true;
                    }
                    else
                    {
                        isLegal = false;
                    }
                }
                else
                {
                    isLegal = false;
                }
            }
            else
            {
                isLegal = false;
            }

            return isLegal;
        }

        public static bool CheckIfEndOfGame()
        {
            bool endOfGame;
            System.Console.WriteLine("Do you wish to play another game? (yes/no)");
            string userInput = System.Console.ReadLine();
            while(userInput != "yes" && userInput != "no")
            {
                System.Console.WriteLine("Do you wish to play another game? (yes/no)");
                userInput = System.Console.ReadLine();
            }

            if(userInput.Equals("yes"))
            {
                endOfGame = false;
            }
            else
            {
                endOfGame = true;
            }

            return endOfGame;
        }

        public static void presentFinalScore(Player i_playerO, Player i_playerX)
        {
            System.Console.WriteLine("The score of " + i_playerO.Name + " is: " + i_playerO.Points);
            System.Console.WriteLine("The score of " + i_playerX.Name + " is: " + i_playerX.Points);
            if (i_playerO.Points > i_playerX.Points)
            {
                System.Console.WriteLine("The winner is " + i_playerO.Name);
            }
            else if (i_playerO.Points < i_playerX.Points)
            {
                System.Console.WriteLine("The winner is " + i_playerX.Name);
            }
            else
            {
                System.Console.WriteLine("It's a tie!");
            }
        }
    }
}
